//
//  SecurityTaskProgressVC.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/5/19.
//

import Foundation
import UIKit

class SecurityTaskProgressVC: SubLevelViewController {
    
    var datas: [BaglRwwcjdLzrModel] = []
    var rwid: String?
    
    var param = SecurityParam()

    init(withDeptId deptId: Int64? = nil, deptName: String? = nil, date: String = Date.todayDate(), dateEnum: CustomDateEnum = .daily, rwid: String? = nil) {
        super.init(withDeptId: deptId, deptName: deptName, date: date, dateEnum: dateEnum)
        self.rwid = rwid
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        title = "各任务完成情况"
        view.backgroundColor = .bg
        
        setupUI()
        
        if deptName == nil {
            deptName = BSUser.currentUser.deptName
            deptId = BSUser.currentUser.deptId
        }
        
        param.deptId = deptId
        param.date = date
        param.dateType = dateEnum.rawValue
        param.rwid = rwid
        
        reloadData()
    }
    
    func reloadData() {
        API.getBaglRwwcjd(withParam: param) { responseModel in
            print("getBaglQueryRwwcjd", responseModel)
            if responseModel.errorCode == .Success {
                self.datas = responseModel.model?.lzrArr ?? []
                DispatchQueue.main.async {
                    self.taskProcessPanel.buildData(withModel: responseModel.model?.rwxq)
                    self.tableView.reloadData()
                    self.tableView.tableShowEmpty(withDataCount: self.datas.count)
                }
            }
        }
    }

    // MARK: - Setup

    func setupUI() {
        view.addSubview(taskProcessPanel)
        view.addSubview(tableView)

        taskProcessPanel.snp.makeConstraints { make in
            make.top.equalTo(view.safeAreaLayoutGuide).offset(10)
            make.left.equalTo(view).offset(10)
            make.right.equalTo(view).offset(-10)
        }
        
        tableView.snp.makeConstraints { make in
            make.left.right.equalTo(view)
            make.top.equalTo(taskProcessPanel.snp.bottom).offset(10)
            make.bottom.equalTo(view)
        }
    }
    
    lazy var taskProcessPanel: SecurityTaskProcessPanel = {
        let panel = SecurityTaskProcessPanel()
        return panel
    }()

    lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false
        tableView.register(SecurityTaskProcessCell.self, forCellReuseIdentifier: "SecurityTaskProcessCell")
        return tableView
    }()
}

extension SecurityTaskProgressVC: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let data = datas[indexPath.row]
        let cell = (tableView.dequeueReusableCell(withIdentifier: "SecurityTaskProcessCell", for: indexPath)) as! SecurityTaskProcessCell
        cell.buildData(data: data)
        cell.selectionStyle = .none
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 58
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let data = datas[indexPath.row]
        if let lzrid = data.lzrid {
            let vc = SecurityTopVC(withDeptId: deptId, deptName: deptName, date: date, dateEnum: dateEnum, ownerId: "\(lzrid)")
            navigationController?.pushViewController(vc, animated: true)
        }
    }
}

